home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / zsh-3.0-p / zsh-3 / zsh-3.0-pre3 / Misc / c2z next >
Text File  |  1996-05-06  |  3KB  |  121 lines

  1. #! /bin/sh
  2. #
  3. # c2z - environment conversion tool
  4. # Contributed by Bart Schaefer
  5. # (Tweaked a bit by Paul Falstad)
  6. #
  7. # This is a quick script to convert csh aliases to zsh aliases/functions.
  8. # It also converts the csh environment and local variables to zsh.  c2z
  9. # uses the csh to parse its own dot-files, then processes csh output to
  10. # convert the csh settings to zsh.
  11. #
  12. # When run as a zsh fuction, c2z runs csh as if it were an interactive
  13. # shell whenever the parent zsh is interactive.  When run as a shell
  14. # script, the -i switch can be used to force this behavior.
  15. #
  16. # The -l (login) switch causes csh to run as if it were a login shell.
  17. # This is done "properly" if c2z is used as a zsh function, otherwise
  18. # it's faked by explicitly sourcing .login.  Use with caution if your
  19. # .login initializes an X server or does other one-time-only startup
  20. # procedures.
  21. #
  22. # usage:
  23. #    c2z [-i] [-l]
  24. #
  25. # You can use this script in your .zshrc or .zlogin files to load your
  26. # regular csh environment into zsh; for example, in .zlogin:
  27. #
  28. #    . =(c2z -l)
  29. #
  30. # This is not perfect, but it gets most common aliases and variables.
  31. # It's also rather time-consuming to do this every time you log in.
  32. # However, if you're moving from csh to zsh for the first time, this
  33. # can get you started with a familiar environment right away.
  34. #
  35. # In case your mailer eats tabs, $T is set to expand to a tab.
  36. #
  37. T="`echo x | tr x '\011'`"
  38.  
  39. # If we're zsh, we can run "- csh" to get the complete environment.
  40. #
  41. MINUS=""
  42. LOGIN=""
  43. INTERACT=""
  44. case "$VERSION" in
  45. zsh*)
  46.     case $1 in
  47.     -l*) MINUS="-" ;;
  48.     -i*) INTERACT="-i" ;;
  49.     esac
  50.     if [[ -o INTERACTIVE ]]; then INTERACT="-i"; fi
  51.     setopt nobanghist
  52.     ;;
  53. *)
  54.     case $1 in
  55.     -l*) LOGIN="source ~/.login" ;;
  56.     -i*) INTERACT="-i" ;;
  57.     esac
  58.     ;;
  59. esac
  60.  
  61. ( eval $MINUS csh $INTERACT ) <<EOF 2>&1 >/dev/null
  62. $LOGIN
  63. alias >! /tmp/cz$$.a
  64. setenv >! /tmp/cz$$.e
  65. set >! /tmp/cz$$.v
  66. EOF
  67.  
  68. # save stdin
  69. exec 9<&0
  70.  
  71. # First convert aliases
  72. exec < /tmp/cz$$.a
  73.  
  74. # Taken straight from ctoz except for $T and "alias --"
  75. sed -e 's/'"$T"'(\(.*\))/'"$T"'\1/' >/tmp/cz$$.1
  76. grep ! /tmp/cz$$.1 >/tmp/cz$$.2
  77. grep -v ! /tmp/cz$$.1 >/tmp/cz$$.3
  78. sed -e "s/'/'"\\\\"''"/g \
  79.     -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/alias -- \1='"'\2'/" \
  80.     /tmp/cz$$.3
  81. sed -e 's/![:#]*/$/g' \
  82.     -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/\1 () { \2 }/' \
  83.     /tmp/cz$$.2
  84.  
  85. # Next, convert environment variables
  86. exec < /tmp/cz$$.e
  87.  
  88. # Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
  89. sed -e '/^SHLVL/d' \
  90.     -e "s/'/'"\\\\"''"/g \
  91.     -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
  92.     -e "s/$/'/"
  93.  
  94. # Finally, convert local variables
  95. exec < /tmp/cz$$.v
  96.  
  97. sed -e 's/'"$T"'/=/' \
  98.     -e "s/'/'"\\\\"''"/g \
  99.     -e '/^[A-Za-z0-9_]*=[^(]/{
  100.     s/=/='"'/"'
  101.     s/$/'"'/"'
  102.     }' |
  103. sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
  104.      -e '/^histchars=/s//HISTCHARS=/' \
  105.      -e '/^history=/s//HISTSIZE=/' \
  106.      -e '/^home=/s//HOME=/' \
  107.      -e '/^ignoreeof=/s/.*/setopt ignoreeof/' \
  108.      -e '/^noclobber=/s/.*/setopt noclobber/' \
  109.      -e '/^notify=/d' \
  110.      -e '/^showdots=/s/.*/setopt globdots/' \
  111.     -e '/^savehist=/s//HISTFILE=\~\/.zhistory SAVEHIST=/' \
  112.      -e '/^autolist=/s/.*/setopt autolist/' \
  113.      -e '/^correct=[cmd]*/s//setopt autocorrect/' \
  114.      -e '/^who=/s//WATCHFMT=/'
  115.  
  116.  
  117. exec 0<&9
  118.  
  119. rm /tmp/cz$$.?
  120. exit
  121.